const Koa = require("koa");
const Router = require("koa-router");
const cors = require("koa-cors");
const mockList = require("./mock/index.js");
const { sleep, random } = require("./utils.js");

const app = new Koa();
const router = new Router();

mockList.forEach((item) => {
  const { url, method, response } = item;
  router[method](url, async (ctx) => {
    const res = response(ctx);
    await sleep(random(500, 800));
    ctx.body = res;
  });
});

app.use(cors());
app.use(router.routes());
app.listen(3001);
